﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;


namespace WebApplication1
{
    public partial class viewAllNewsByType : System.Web.UI.Page
    {
        AdminAL adminAL;

        protected int cmb_ppa_currentPageNumber = 1;
        protected int clg_acdm_currentPageNumber = 1;
        protected int clg_sprt_currentPageNumber = 1;
        protected int clg_othr_currentPageNumber = 1;
        protected int othr_ppa_currentPageNumber = 1;
        protected int obtry_currentPageNumber = 1;
        protected int othr_currentPageNumber = 1;

        protected int CMB_PPA_PER_ROW = 1;
        protected int CLG_ACDM_PER_ROW = 1;
        protected int CLG_SPRT_PER_ROW = 1;
        protected int CLG_OTHR_PER_ROW = 1;
        protected int OTHR_PPA_PER_ROW = 1;
        protected int OBTRY_PER_ROW = 1;
        protected int OTHR_PER_ROW = 1;

        private const int CMB_PPA_PAGE_SIZE = 3;
        private const int CLG_ACDM_PAGE_SIZE = 3;
        private const int CLG_SPRT_PAGE_SIZE = 3;
        private const int CLG_OTHR_PAGE_SIZE = 3;
        private const int OTHR_PPA_PAGE_SIZE = 3;
        private const int OBTRY_PAGE_SIZE = 3;
        private const int OTHR_PAGE_SIZE = 3;

        int PAGE_SIZE=0;
        int NEWS_PER_ROW=0;
        List<NewsItemDTO> allNewsData=null;
        string yrWiseQueryString;
        

        protected void Page_Load(object sender, EventArgs e)

        {
            BindData("CMB_PPA");
            BindData("CLG_ACDM");
            BindData("CLG_SPRT");
            BindData("CLG_OTHR");
            BindData("OTHR_PPA");
            BindData("OBTRY");
            BindData("OTHR");
 
        }


        private void BindData(string newsType)
        {

            adminAL = new AdminAL();
            allNewsData = new List<NewsItemDTO>();
            List<NewsItemDTO> allNewsDataOfOnePage = new List<NewsItemDTO>();

            Button btnNewsFirst =new Button();
            Button btnNewsPrev = new Button();
            Button btnNewsNext = new Button();
            Button btnNewsLast = new Button();

            Label lblNewsPage = new Label();
            Label lblNewsCurrPage = new Label();
            Label lblNewsOf = new Label();
            Label lblNewsTotalPage = new Label();

            int currentPageNumber=0;
            

            double totalRows=0.0;

            if (newsType.Equals("CMB_PPA"))
            {
                allNewsData = adminAL.GetAllNews().Where(a => a.Type == "Col PPA News" ).Where(b =>b.Visible = true).OrderByDescending(c => c.ModifiedTime).ToList();

                lblAllColPPAPage.Text = "Displaying Colombo PPA News - Page ";
                lblAllColPPAOf.Text = " of ";

                btnNewsFirst= btnAllColPPAFirst;
                btnNewsPrev = btnAllColPPAPrev;
                btnNewsNext = btnAllColPPANext;
                btnNewsLast = btnAllColPPALast;

                lblNewsPage = lblAllColPPAPage;
                lblNewsCurrPage = lblAllColPPACurrPage;
                lblNewsOf = lblAllColPPAOf;
                lblNewsTotalPage = lblAllColPPATotalPage;

                PAGE_SIZE = CMB_PPA_PAGE_SIZE;
                NEWS_PER_ROW = CMB_PPA_PER_ROW;
               
                if (Session["cmb_ppa_currentPageNumber"] != null)
                {
                    currentPageNumber = Convert.ToInt32(Session["cmb_ppa_currentPageNumber"]);
                }
                else
                {
                    currentPageNumber = cmb_ppa_currentPageNumber;
                }

                totalRows = (int)allNewsData.Count;
                lblNewsTotalPage.Text = CalculateTotalPages(totalRows).ToString();
            }
            else if (newsType.Equals("CLG_ACDM"))
            {
                allNewsData = adminAL.GetAllNews().Where(a => a.Type == "College Academic").Where(b => b.Visible = true).OrderByDescending(c => c.ModifiedTime).ToList();

                lblAllColgAcdmPage.Text = "Displaying College Academic News - Page ";
                lblAllColgAcdmOf.Text = " of ";

                btnNewsFirst = btnAllColgAcdmFirst;
                btnNewsPrev = btnAllColgAcdmPrev;
                btnNewsNext = btnAllColgAcdmNext;
                btnNewsLast = btnAllColgAcdmLast;

                lblNewsPage = lblAllColgAcdmPage;
                lblNewsCurrPage = lblAllColgAcdmCurrPage;
                lblNewsOf = lblAllColgAcdmOf;
                lblNewsTotalPage = lblAllColgAcdmTotalPage;

                PAGE_SIZE = CLG_ACDM_PAGE_SIZE;
                NEWS_PER_ROW = CLG_ACDM_PER_ROW;

                if (Session["clg_acdm_currentPageNumber"] != null)
                {
                    currentPageNumber = Convert.ToInt32(Session["clg_acdm_currentPageNumber"]);
                }
                else
                {
                    currentPageNumber = clg_acdm_currentPageNumber;
                }

                totalRows = (int)allNewsData.Count;
                lblNewsTotalPage.Text = CalculateTotalPages(totalRows).ToString();

            }
            else if (newsType.Equals("CLG_SPRT"))
            {
                allNewsData = adminAL.GetAllNews().Where(a => a.Type == "College Sports").Where(b => b.Visible = true).OrderByDescending(c => c.ModifiedTime).ToList();

                lblAllColgSprtPage.Text = "Displaying College Sports News - Page ";
                lblAllColgSprtOf.Text = " of ";

                btnNewsFirst= btnAllColgSprtFirst;
                btnNewsPrev = btnAllColgSprtPrev;
                btnNewsNext = btnAllColgSprtNext;
                btnNewsLast = btnAllColgSprtLast;

                lblNewsPage = lblAllColgSprtPage;
                lblNewsCurrPage = lblAllColgSprtCurrPage;
                lblNewsOf = lblAllColgSprtOf;
                lblNewsTotalPage = lblAllColgSprtTotalPage;

                PAGE_SIZE = CLG_SPRT_PAGE_SIZE;
                NEWS_PER_ROW = CLG_SPRT_PER_ROW;

                if (Session["clg_sprt_currentPageNumber"] != null)
                {
                    currentPageNumber = Convert.ToInt32(Session["clg_sprt_currentPageNumber"]);
                }
                else
                {
                    currentPageNumber = clg_sprt_currentPageNumber;
                }

                totalRows = (int)allNewsData.Count;
                lblNewsTotalPage.Text = CalculateTotalPages(totalRows).ToString();


            }
            else if (newsType.Equals("CLG_OTHR"))
            {
                allNewsData = adminAL.GetAllNews().Where(a => a.Type == "College Other").Where(b => b.Visible = true).OrderByDescending(c => c.ModifiedTime).ToList();

                lblAllColgOthrPage.Text = "Displaying College other News - Page ";
                lblAllColgOthrOfPage.Text = " of ";

                btnNewsFirst = btnAllColgOthrFirst;
                btnNewsPrev = btnAllColgOthrPrev;
                btnNewsNext = btnAllColgOthrNext;
                btnNewsLast = btnAllColgOthrLast;

                lblNewsPage = lblAllColgOthrPage;
                lblNewsCurrPage = lblAllColgOthrCurrPage;
                lblNewsOf = lblAllColgOthrOfPage;
                lblNewsTotalPage = lblAllColgOthrTotalPage;

                PAGE_SIZE = CLG_OTHR_PAGE_SIZE;
                NEWS_PER_ROW = CLG_OTHR_PER_ROW;

                if (Session["clg_othr_currentPageNumber"] != null)
                {
                    currentPageNumber = Convert.ToInt32(Session["clg_othr_currentPageNumber"]);
                }
                else
                {
                    currentPageNumber = clg_othr_currentPageNumber;
                }

                totalRows = (int)allNewsData.Count;
                lblNewsTotalPage.Text = CalculateTotalPages(totalRows).ToString();


            }
            else if (newsType.Equals("OTHR_PPA"))
            {
                allNewsData = adminAL.GetAllNews().Where(a => a.Type == "OtherPPANews").Where(b => b.Visible = true).OrderByDescending(c => c.ModifiedTime).ToList();

                lblAllOthrPPAPage.Text = "Displaying Oher  PPAs's News around the globe - Page ";
                lblAllOthrPPAOf.Text = " of ";

                btnNewsFirst = btnAllOthrPPAFirst;
                btnNewsPrev = btnAllOthrPPAPrev;
                btnNewsNext = btnAllOthrPPANext;
                btnNewsLast = btnAllOthrPPALast;

                lblNewsPage = lblAllOthrPPAPage;
                lblNewsCurrPage = lblAllOthrPPACurrPage;
                lblNewsOf = lblAllOthrPPAOf;
                lblNewsTotalPage = lblAllOthrPPATotalPage;

                PAGE_SIZE = OTHR_PAGE_SIZE;
                NEWS_PER_ROW = OTHR_PER_ROW;

                if (Session["othr_ppa_currentPageNumber"] != null)
                {
                    currentPageNumber = Convert.ToInt32(Session["othr_ppa_currentPageNumber"]);
                }
                else
                {
                    currentPageNumber = othr_ppa_currentPageNumber;
                }

                totalRows = (int)allNewsData.Count;
                lblNewsTotalPage.Text = CalculateTotalPages(totalRows).ToString();

            }


            else if (newsType.Equals("OBTRY"))
            {
                allNewsData = adminAL.GetAllNews().Where(a => a.Type == "Obituary").Where(b => b.Visible = true).OrderByDescending(c => c.ModifiedTime).ToList();

                lblAllObtryPage.Text = "Displaying Obituary News - Page ";
                lblAllObtryOf.Text = " of ";

                btnNewsFirst = btnAllObtryFirst;
                btnNewsPrev = btnAllObtryPrev;
                btnNewsNext = btnAllObtryNext;
                btnNewsLast = btnAllObtryLast;

                lblNewsPage = lblAllObtryPage;
                lblNewsCurrPage = lblAllObtryCurrPage;
                lblNewsOf = lblAllObtryOf;
                lblNewsTotalPage = lblAllObtryTotalPage;

                PAGE_SIZE = OBTRY_PAGE_SIZE;
                NEWS_PER_ROW = OBTRY_PER_ROW;

                if (Session["obtry_currentPageNumber"] != null)
                {
                    currentPageNumber = Convert.ToInt32(Session["obtry_currentPageNumber"]);
                }
                else
                {
                    currentPageNumber = obtry_currentPageNumber;
                }

                totalRows = (int)allNewsData.Count;
                lblNewsTotalPage.Text = CalculateTotalPages(totalRows).ToString();

            }
            else if (newsType.Equals("OTHR"))
            {
                allNewsData = adminAL.GetAllNews().Where(a => a.Type == "Other").Where(b => b.Visible = true).OrderByDescending(c => c.ModifiedTime).ToList();

                lblAllOthrPage.Text = "Displaying Other News - Page ";
                lblAllOthrOf.Text = " of ";

                btnNewsFirst = btnAllOthrFirst;
                btnNewsPrev = btnAllOthrPrev;
                btnNewsNext = btnAllOthrNext;
                btnNewsLast = btnAllOthrLast;

                lblNewsPage = lblAllOthrPage;
                lblNewsCurrPage = lblAllOthrCurrPage;
                lblNewsOf = lblAllOthrOf;
                lblNewsTotalPage = lblAllOthrotalPage;

                PAGE_SIZE = OTHR_PAGE_SIZE;
                NEWS_PER_ROW = OTHR_PER_ROW;

                if (Session["othr_currentPageNumber"] != null)
                {
                    currentPageNumber = Convert.ToInt32(Session["othr_currentPageNumber"]);
                }
                else
                {
                    currentPageNumber = othr_currentPageNumber;
                }

                totalRows = (int)allNewsData.Count;
                lblNewsTotalPage.Text = CalculateTotalPages(totalRows).ToString();
            }
            
           
           


            

          
            lblNewsCurrPage.Text = currentPageNumber.ToString();


            if (Convert.ToInt32(lblNewsTotalPage.Text) < 3)
            {
                if (Int32.Parse(lblNewsTotalPage.Text) <= 0)
                {
                    btnNewsFirst.Enabled = false;
                    btnNewsPrev.Enabled = false;
                    btnNewsNext.Enabled = false;
                    btnNewsLast.Enabled = false;


                }

                if (currentPageNumber == 1)
                {
                    btnNewsFirst.Enabled = false;
                    btnNewsPrev.Enabled = false;



                    if (Int32.Parse(lblNewsTotalPage.Text) == 1)
                    {
                        allNewsDataOfOnePage = allNewsData.GetRange(0, Convert.ToInt32(totalRows));

                        btnNewsNext.Enabled = false;
                        btnNewsLast.Enabled = false;

                    }
                    else if (Int32.Parse(lblNewsTotalPage.Text) == 2)
                    {
                        allNewsDataOfOnePage = allNewsData.GetRange(0, PAGE_SIZE * NEWS_PER_ROW);

                        btnNewsNext.Enabled = true;
                        btnNewsLast.Enabled = true;

                    }
                }

                else
                {

                    btnNewsPrev.Enabled = true;
                    btnNewsFirst.Enabled = true;

                    if (currentPageNumber == Int32.Parse(lblNewsTotalPage.Text))
                    {
                        allNewsDataOfOnePage = allNewsData.GetRange((currentPageNumber - 1) * PAGE_SIZE * NEWS_PER_ROW
                            , (Convert.ToInt32(totalRows) - (currentPageNumber - 1) * PAGE_SIZE * NEWS_PER_ROW));

                        btnNewsNext.Enabled = false;
                        btnNewsLast.Enabled = false;

                    }
                    else
                    {
                        allNewsDataOfOnePage = allNewsData.GetRange((currentPageNumber - 1) * PAGE_SIZE * NEWS_PER_ROW, PAGE_SIZE * NEWS_PER_ROW);

                        btnNewsNext.Enabled = true;
                        btnNewsLast.Enabled = true;


                    }

                }
            }


            else
            {
                if (currentPageNumber == 1)
                {
                    allNewsDataOfOnePage = allNewsData.GetRange(0, PAGE_SIZE * NEWS_PER_ROW);

                    btnNewsFirst.Enabled = false;
                    btnNewsPrev.Enabled = false;
                    btnNewsNext.Enabled = true;
                    btnNewsLast.Enabled = true;

                }



                else
                {
                    btnNewsPrev.Enabled = true;
                    btnNewsFirst.Enabled = true;

                    if (currentPageNumber == Int32.Parse(lblNewsTotalPage.Text))
                    {
                        allNewsDataOfOnePage = allNewsData.GetRange((currentPageNumber - 1) * PAGE_SIZE * NEWS_PER_ROW
                            , (Convert.ToInt32(totalRows) - (currentPageNumber - 1) * PAGE_SIZE * NEWS_PER_ROW));

                        btnNewsNext.Enabled = false;
                        btnNewsLast.Enabled = false;

                    }
                    else
                    {
                        allNewsDataOfOnePage = allNewsData.GetRange((currentPageNumber - 1) * PAGE_SIZE * NEWS_PER_ROW, PAGE_SIZE * NEWS_PER_ROW);

                        btnNewsNext.Enabled = true;
                        btnNewsLast.Enabled = true;


                    }





                }
            }

            CreateDynamicTable(allNewsDataOfOnePage, newsType);
        



        }


        private int CalculateTotalPages(double totalRows)
        {
            int totalPages = 0;
            
                totalPages = (int)Math.Ceiling(totalRows / (PAGE_SIZE * NEWS_PER_ROW));
           

            return totalPages;
        }

        protected void ChangePageCMBPPA(object sender, CommandEventArgs e)
        {

            switch (e.CommandName)
            {

                case "First":
                    cmb_ppa_currentPageNumber = 1;
                    break;

                case "Previous":
                    cmb_ppa_currentPageNumber = Int32.Parse(lblAllColPPACurrPage.Text) - 1;
                    break;

                case "Next":
                    cmb_ppa_currentPageNumber = Int32.Parse(lblAllColPPACurrPage.Text) + 1;
                    break;
                case "Last":
                    cmb_ppa_currentPageNumber = Int32.Parse(lblAllColPPATotalPage.Text);
                    break;
            }

           
            Session["cmb_ppa_currentPageNumber"] = cmb_ppa_currentPageNumber;

            BindData("CMB_PPA");
        }

        protected void ChangePageCOLACA(object sender, CommandEventArgs e)
        {

            switch (e.CommandName)
            {

                case "First":
                    clg_acdm_currentPageNumber = 1;
                    break;

                case "Previous":
                    clg_acdm_currentPageNumber = Int32.Parse(lblAllColgAcdmCurrPage.Text) - 1;
                    break;

                case "Next":
                    clg_acdm_currentPageNumber = Int32.Parse(lblAllColgAcdmCurrPage.Text) + 1;
                    break;
                case "Last":
                    clg_acdm_currentPageNumber = Int32.Parse(lblAllColgAcdmTotalPage.Text);
                    break;
            }


            Session["clg_acdm_currentPageNumber"] = clg_acdm_currentPageNumber;

            BindData("CLG_ACDM");
        }

        protected void ChangePageCOLSPRT(object sender, CommandEventArgs e)
        {

            switch (e.CommandName)
            {

                case "First":
                    clg_sprt_currentPageNumber = 1;
                    break;

                case "Previous":
                    clg_sprt_currentPageNumber = Int32.Parse(lblAllColgSprtCurrPage.Text) - 1;
                    break;

                case "Next":
                    clg_sprt_currentPageNumber = Int32.Parse(lblAllColgSprtCurrPage.Text) + 1;
                    break;
                case "Last":
                    clg_sprt_currentPageNumber = Int32.Parse(lblAllColgSprtTotalPage.Text);
                    break;
            }


            Session["clg_sprt_currentPageNumber"] = clg_sprt_currentPageNumber;

            BindData("CLG_SPRT");
        }

        protected void ChangePageCOLOTHR(object sender, CommandEventArgs e)
        {

            switch (e.CommandName)
            {

                case "First":
                    clg_othr_currentPageNumber = 1;
                    break;

                case "Previous":
                    clg_othr_currentPageNumber = Int32.Parse(lblAllColgOthrCurrPage.Text) - 1;
                    break;

                case "Next":
                    clg_othr_currentPageNumber = Int32.Parse(lblAllColgOthrCurrPage.Text) + 1;
                    break;
                case "Last":
                    clg_othr_currentPageNumber = Int32.Parse(lblAllColgOthrTotalPage.Text);
                    break;
            }


            Session["clg_othr_currentPageNumber"] = clg_othr_currentPageNumber;

            BindData("CLG_OTHR");
        }

        protected void ChangePageOTHRPPA(object sender, CommandEventArgs e)
        {

            switch (e.CommandName)
            {

                case "First":
                    othr_ppa_currentPageNumber = 1;
                    break;

                case "Previous":
                    othr_ppa_currentPageNumber = Int32.Parse(lblAllOthrPPACurrPage.Text) - 1;
                    break;

                case "Next":
                    othr_ppa_currentPageNumber = Int32.Parse(lblAllOthrPPACurrPage.Text) + 1;
                    break;
                case "Last":
                    othr_ppa_currentPageNumber = Int32.Parse(lblAllOthrPPATotalPage.Text);
                    break;
            }


            Session["othr_ppa_currentPageNumber"] = othr_ppa_currentPageNumber;

            BindData("OTHR_PPA");
        }

        protected void ChangePageOBITUARY(object sender, CommandEventArgs e)
        {

            switch (e.CommandName)
            {

                case "First":
                    obtry_currentPageNumber = 1;
                    break;

                case "Previous":
                    obtry_currentPageNumber = Int32.Parse(lblAllObtryCurrPage.Text) - 1;
                    break;

                case "Next":
                    obtry_currentPageNumber = Int32.Parse(lblAllObtryCurrPage.Text) + 1;
                    break;
                case "Last":
                    obtry_currentPageNumber = Int32.Parse(lblAllObtryTotalPage.Text);
                    break;
            }


            Session["obtry_currentPageNumber"] = obtry_currentPageNumber;

            BindData("OBTRY");
        }

        protected void ChangePageOther(object sender, CommandEventArgs e)
        {

            switch (e.CommandName)
            {

                case "First":
                    othr_currentPageNumber = 1;
                    break;

                case "Previous":
                    othr_currentPageNumber = Int32.Parse(lblAllOthrCurrPage.Text) - 1;
                    break;

                case "Next":
                    othr_currentPageNumber = Int32.Parse(lblAllOthrCurrPage.Text) + 1;
                    break;
                case "Last":
                    othr_currentPageNumber = Int32.Parse(lblAllOthrotalPage.Text);
                    break;
            }


            Session["othr_currentPageNumber"] = othr_currentPageNumber;

            BindData("OTHR");
        }


        private void CreateDynamicTable(List<NewsItemDTO> allNewsDataOfOnePage,string newsType)
        {
            Table tbl = new Table();
            tbl.Width = new Unit("100%");
            tbl.BorderStyle = BorderStyle.Solid;

            if (newsType.Equals("CMB_PPA"))
            {
                phCmbPPA.Controls.Clear();
                phCmbPPA.Controls.Add(tbl);
            }
            else if (newsType.Equals("CLG_ACDM"))
            {
                phClgAcdm.Controls.Clear();
                phClgAcdm.Controls.Add(tbl);
            }
            else if (newsType.Equals("CLG_SPRT"))
            {
                phClgSprt.Controls.Clear();
                phClgSprt.Controls.Add(tbl);
            }
            else if (newsType.Equals("CLG_OTHR"))
            {
                phClgOthr.Controls.Clear();
                phClgOthr.Controls.Add(tbl);
            }
            else if (newsType.Equals("OTHR_PPA"))
            {
                phOtherPPA.Controls.Clear();
                phOtherPPA.Controls.Add(tbl);
            }
            else if (newsType.Equals("OBTRY"))
            {
                phObtry.Controls.Clear();
                phObtry.Controls.Add(tbl);
            }
            else if (newsType.Equals("OTHR"))
            {
                phOtht.Controls.Clear();
                phOtht.Controls.Add(tbl);
            }
            

            // Fetch the number of Rows and Columns for the table 
            // using the properties


            int tblRows = allNewsDataOfOnePage.Count / NEWS_PER_ROW;
            int tblCols = 0;


            // Create a Table and set its properties 
           
            // Add the table to the placeholder control
            
            // Now iterate through the table and add your controls 
            for (int i = 0; i <= tblRows; i++)
            {
                TableRow tr = new TableRow();
                tr.Width = new Unit("25%");



                if (i != tblRows && tblRows >= 0)
                {
                    tblCols = NEWS_PER_ROW;
                }
                else
                {
                    tblCols = allNewsDataOfOnePage.Count % NEWS_PER_ROW;
                }


                for (int j = 0; j < tblCols; j++)
                {
                    TableCell tc = new TableCell();

                    HyperLink hlAthleticYrCell = new HyperLink();



                    hlAthleticYrCell.Text = allNewsDataOfOnePage[i * NEWS_PER_ROW + j].Title;
                     if (string.IsNullOrEmpty(yrWiseQueryString))
                    {
                        hlAthleticYrCell.NavigateUrl = "viewNews.aspx?" +  allNewsDataOfOnePage[i * NEWS_PER_ROW + j].NewsId;
                    }
                    if (i % 2 == 0)
                    {
                        tc.Style.Add("background-color", "#003399");
                        tc.ForeColor = System.Drawing.Color.White;
                    }
                    else
                    {
                        tc.Style.Add("background-color", "White");
                        tc.ForeColor = System.Drawing.Color.Blue;
                    }



                    // Add the control to the TableCell
                    tc.Controls.Add(hlAthleticYrCell);
                    // Add the TableCell to the TableRow
                    tr.Cells.Add(tc);
                }

                // Add the TableRow to the Table
                tbl.Rows.Add(tr);
            }

            // This parameter helps determine in the LoadViewState event,
            // whether to recreate the dynamic controls or not

            ViewState["dynamictable"] = true;
        }

        protected void gvCollegeSports_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


        
    }
}
